/*
 * @(#)DownloadResultTestCase.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.result;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;

import com.opensymphony.xwork.ActionContext;

import com.nhncorp.lucy.common.config.model.ApplicationInfo;
import com.nhncorp.lucy.web.helper.ServletHelper;
import com.nhncorp.usf.core.config.runtime.ResultPageInfo;
import com.nhncorp.usf.core.config.runtime.ResultPageInfo.DISPATCHER_TYPE;
import com.nhncorp.usf.core.result.direct.DownloadResult;
import com.nhncorp.usf.core.servlet.ServletContextHolder;
import junit.framework.Assert;

/**
 * {@link com.nhncorp.usf.core.result.direct.HtmlResult} TestCase.
 *
 * @author Web Platform Development Team
 */
public class DownloadResultTestCase {
    AbstractResult directResult = new DownloadResult();
    ResultPageInfo resultPageInfo;

    public static void initialize() throws Exception {
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        ActionContext.getContext().put(ServletHelper.HTTP_REQUEST, request);
        ActionContext.getContext().put(ServletHelper.HTTP_RESPONSE, response);
        ServletContextHolder.set(new MockServletContext());
        ApplicationInfo.setPageEncoding("utf-8");
    }

    @Test
    public void downloadResultTest() throws Exception {
        initialize();

        resultPageInfo = new ResultPageInfo(DISPATCHER_TYPE.direct, null);
        resultPageInfo.setId("dummy_id");
        resultPageInfo.getProperties().setProperty("contentType", "text/html");
        resultPageInfo.getProperties().setProperty("language", "download");
        resultPageInfo.getProperties().setProperty("src", "result");
        resultPageInfo.getProperties().setProperty("serverFileName", "phisicalName");
        resultPageInfo.getProperties().setProperty("downloadFileName", "logicalName");

        BeanUtils.populate(directResult, resultPageInfo.getProperties());

        MockHttpServletResponse response = (MockHttpServletResponse)ServletHelper.getResponse();
        try {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("phisicalName", "test.jar");
            resultMap.put("logicalName", "test.org");
            dataMap.put("result", resultMap);

            directResult.execute(resultPageInfo, dataMap);
            String contents = (String)response.getHeader("Content-Disposition");

            Assert.assertEquals("attachment; filename=\"test.org\"", contents);
        } catch(Exception except) {
            Assert.fail();
        }
    }

    @Test
    public void downloadResult1Test() throws Exception {
        initialize();

        resultPageInfo = new ResultPageInfo(DISPATCHER_TYPE.direct, null);
        resultPageInfo.setId("dummy_id");
        resultPageInfo.getProperties().setProperty("contentType", "text/html");
        resultPageInfo.getProperties().setProperty("language", "download");
        resultPageInfo.getProperties().setProperty("src", "result");
        resultPageInfo.getProperties().setProperty("serverFileName", "phisicalName");
        resultPageInfo.getProperties().setProperty("downloadFileName", "logicalName");

        BeanUtils.populate(directResult, resultPageInfo.getProperties());

        MockHttpServletResponse response = (MockHttpServletResponse)ServletHelper.getResponse();
        try {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("phisicalName", System.getProperty("user.dir") + File.separator + "target" + File.separator + "test-classes" + File.separator + "test.tmm");
            resultMap.put("logicalName", System.getProperty("user.dir") + File.separator + "target" + File.separator + "test-classes" + File.separator + "test.org");
            dataMap.put("result", resultMap);

            directResult.execute(resultPageInfo, dataMap);
            String contents = (String)response.getHeader("Content-Disposition");

            Assert.assertEquals("attachment; filename=\"" + System.getProperty("user.dir") + File.separator + "target" + File.separator + "test-classes" + File.separator + "test.org\"", contents);
        } catch(Exception except) {
            Assert.fail();
        }
    }
}